#### Is climate change a valid reason for migration? Evidence from a conjoint experiment
#### Replication files
#### Michael Faure, Jaroslaw Kantorowicz, and Akiva Weiss

library(cjoint)
library(cregg)
library(ggpubr)
library(ggsci)
library(vtable)

### Loading conjoint migrants

conjoint_dataset <- readRDS("conjoint_dataset.RDS")

#### Converting into English attributes' levels

# Renaming levels considering Economic situation

ftable(conjoint_dataset$Economische.situatie)

conjoint_dataset <- conjoint_dataset %>% mutate(Economic_situation=ifelse(Economische.situatie=="In staat om zichzelf te onderhouden", "Able to sustain himself/herself",
                                                                          ifelse(Economische.situatie=="Niet in staat om zichzelf te onderhouden", "Not able to sustain himself/herself",
                                                                                 ifelse(Economische.situatie=="Onwaarschijnlijk in staat om zichzelf te onderhouden", "Unlikely able to sustain himself/herself",
                                                                                        ifelse(Economische.situatie=="Waarschijnlijk in staat zichzelf te onderhouden", "Likely able to sustain himself/herself", NA)))))

conjoint_dataset$Economic_situation <- factor(conjoint_dataset$Economic_situation, levels = c("Not able to sustain himself/herself",
                                                                                              "Unlikely able to sustain himself/herself",
                                                                                              "Likely able to sustain himself/herself",
                                                                                              "Able to sustain himself/herself"))

# Renaming levels considering Gender

ftable(conjoint_dataset$Geslacht)


conjoint_dataset <- conjoint_dataset %>% mutate(Gender=ifelse(Geslacht=="Man", "Male",
                                                              ifelse(Geslacht=="Vrouw", "Female", NA)))


conjoint_dataset$Gender <- factor(conjoint_dataset$Gender, levels = c("Male",
                                                                      "Female"))


# Renaming levels considering Countr of origin

ftable(conjoint_dataset$Land.van.herkomst)


conjoint_dataset <- conjoint_dataset %>% mutate(Country_of_origin=ifelse(Land.van.herkomst=="Maldiven", "Maledives",
                                                                         ifelse(Land.van.herkomst=="Myanmar", "Myanmar", 
                                                                                ifelse(Land.van.herkomst=="Nigeria", "Nigeria",
                                                                                       ifelse(Land.van.herkomst=="Pakistan", "Pakistan",
                                                                                              ifelse(Land.van.herkomst=="Syrië", "Syria",
                                                                                                     ifelse(Land.van.herkomst=="Vietnam", "Vietnam", NA)))))))


conjoint_dataset$Country_of_origin <- factor(conjoint_dataset$Country_of_origin, levels = c("Nigeria",
                                                                                            "Maledives", "Pakistan", "Syria", "Vietnam", "Myanmar"))




# Renaming levels considering Age

ftable(conjoint_dataset$Leeftijd)


conjoint_dataset <- conjoint_dataset %>% mutate(Age=ifelse(Leeftijd=="18-25 jaar", "18-25 years old",
                                                           ifelse(Leeftijd=="34-48 jaar", "34-48 years old", 
                                                                  ifelse(Leeftijd=="52-65 jaar", "52-64 years old",
                                                                         ifelse(Leeftijd=="65 jaar of ouder", "65 years old or older", NA)))))


conjoint_dataset$Age <- factor(conjoint_dataset$Age, levels = c("65 years old or older", "52-64 years old", "34-48 years old", "18-25 years old"))




# Renaming levels considering Education

ftable(conjoint_dataset$Onderwijsniveau)


conjoint_dataset <- conjoint_dataset %>% mutate(Education=ifelse(Onderwijsniveau=="Basisschool niet voltooid", "Not completed primary education",
                                                                 ifelse(Onderwijsniveau=="Basisschool voltooid", "Primary school completed", 
                                                                        ifelse(Onderwijsniveau=="Middelbare school voltooid", "Secondary school completed",
                                                                               ifelse(Onderwijsniveau=="Universitair diploma", "University degree", NA)))))


conjoint_dataset$Education <- factor(conjoint_dataset$Education, levels = c("Not completed primary education", "Primary school completed", "Secondary school completed", "University degree"))



class(conjoint_dataset$Education)

# Renaming levels considering Reason of migration

ftable(conjoint_dataset$Reden.voor.migratie)


conjoint_dataset <- conjoint_dataset %>% mutate(Reason_of_migration=ifelse(Reden.voor.migratie=="Het ontkomen aan politieke/religieuze/etnische vervolging", "Escaping persecution",
                                                                           ifelse(Reden.voor.migratie=="Het verbeteren van economische kansen", "Seeking economic opportunities", 
                                                                                  ifelse(Reden.voor.migratie=="Vluchten door aardbevingen", "Fleeing earthquake",
                                                                                         ifelse(Reden.voor.migratie=="Vluchten door extreme droogte", "Fleeing drought", 
                                                                                                ifelse(Reden.voor.migratie=="Vluchten door overstromingen", "Fleeing flood", NA))))))


conjoint_dataset$Reason_of_migration <- factor(conjoint_dataset$Reason_of_migration, levels = c("Seeking economic opportunities",
                                                                                                "Fleeing drought",
                                                                                                "Fleeing earthquake", 
                                                                                                "Fleeing flood",
                                                                                                "Escaping persecution"))



class(conjoint_dataset$Reason_of_migration)

attr(conjoint_dataset$Reason_of_migration, "label") <- "Reason of migration"
attr(conjoint_dataset$Economic_situation, "label") <- "Economic situation"
attr(conjoint_dataset$Country_of_origin, "label") <- "Country of origin"

### Diagnostics

### Profile order effect

conjoint_dataset$profile_fac <- as.factor(conjoint_dataset$profile)

plot(cj(conjoint_dataset, selected ~ 
          Reason_of_migration +
          Country_of_origin +
          Economic_situation +
          Education +
          Age +
          Gender,
        id = ~Response.ID, by = ~profile_fac, estimate = "mm"), group = "profile_fac", vline = 0.5)


cj_anova(conjoint_dataset, selected ~ 
           Reason_of_migration +
           Country_of_origin +
           Economic_situation +
           Education +
           Age +
           Gender,
         id = ~Response.ID, by = ~profile_fac)


### Carry-over effect

conjoint_dataset$task_fac <- as.factor(conjoint_dataset$task)

plot(cj(conjoint_dataset, selected ~ 
          Reason_of_migration +
          Country_of_origin +
          Economic_situation +
          Education +
          Age +
          Gender,
        id = ~Response.ID, by = ~task_fac, estimate = "mm"), group = "task_fac", vline = 0.5)


cj_anova(conjoint_dataset, selected ~ 
           Reason_of_migration +
           Country_of_origin +
           Economic_situation +
           Education +
           Age +
           Gender,
         id = ~Response.ID, by = ~task_fac)


### Baseline analysis ### Baseline analysis ### Baseline analysis
### Marginal Means ### Marginal Means ### Marginal Means

marginal_means <- mm(conjoint_dataset, selected ~ 
                       Reason_of_migration +
                       Economic_situation +
                       Education +
                       Age +
                       Gender +
                       Country_of_origin, 
                     id = ~Response.ID,
                     weights = ~weight)

plot(cregg::mm(conjoint_dataset, selected ~ 
                 Reason_of_migration +
                 Economic_situation +
                 Education +
                 Age +
                 Gender +
                 Country_of_origin, 
               id = ~Response.ID,
               weights = ~weight), 
     size = 1,
     feature_headers = FALSE) +
  ggplot2::facet_wrap(~feature, 
                      ncol = 1L,
                      scales = "free_y", 
                      strip.position = "top") + 
  xlim(0.27, 0.7) + 
  theme_bw() +
  theme(legend.position = "none",
        legend.text = element_text(size=7),
        legend.title = element_text(size=7)) +
  scale_color_grey(start = 0.2, end = 0.2) +
  theme(strip.text.x = element_text(size = 9, face = "bold"))


ggsave("main_results_mm.png" , 
       scale = 0.8,
       width = 7,
       height = 9,
       dpi = 500)

### Average Marginal Component Effect ### Average Marginal Component Effect ### Average Marginal Component Effect

amce <- amce(conjoint_dataset, selected ~ 
               Reason_of_migration +
               Economic_situation +
               Education +
               Age +
               Gender +
               Country_of_origin, 
             id = ~Response.ID, 
             weights = ~weight)

plot(cregg::amce(conjoint_dataset, selected ~ 
                 Reason_of_migration +
                 Economic_situation +
                 Education +
                 Age +
                 Gender +
                 Country_of_origin, 
               id = ~Response.ID,
               weights = ~weight), 
     size = 1,
     feature_headers = FALSE) +
  ggplot2::facet_wrap(~feature, 
                      ncol = 1L,
                      scales = "free_y", 
                      strip.position = "top") + 
  xlim(-0.05, 0.4) + 
  theme_bw() +
  theme(legend.position = "none",
        legend.text = element_text(size=7),
        legend.title = element_text(size=7)) +
  scale_color_grey(start = 0.2, end = 0.2) +
  theme(strip.text.x = element_text(size = 9, face = "bold"))

ggsave("main_results_amce.png" , 
       scale = 0.8,
       width = 7,
       height = 9,
       dpi = 500)

# Testing the difference between floods and droughts

conjoint_dataset$Reason_of_migration_releveled <- factor(conjoint_dataset$Reason_of_migration,
                                                         levels = c("Fleeing drought", 
                                                                    "Fleeing flood",
                                                                    "Fleeing earthquake",
                                                                    "Seeking economic opportunities",
                                                                    "Escaping persecution"))


options(scipen=999)

amce_drought <-  amce(conjoint_dataset, selected ~ 
                             Reason_of_migration_releveled +
                             Economic_situation +
                             Education +
                             Age +
                             Gender +
                             Country_of_origin, 
                           id = ~Response.ID,
                           weights = ~weight)


conjoint_dataset$Reason_of_migration_releveled <- factor(conjoint_dataset$Reason_of_migration,
                                                         levels = c("Fleeing flood", 
                                                                    "Fleeing drought",
                                                                    "Fleeing earthquake",
                                                                    "Seeking economic opportunities",
                                                                    "Escaping persecution"))


options(scipen=999)

amce_floods <-  amce(conjoint_dataset, selected ~ 
                             Reason_of_migration_releveled +
                             Economic_situation +
                             Education +
                             Age +
                             Gender +
                             Country_of_origin, 
                           id = ~Response.ID,
                           weights = ~weight)




### Subgroup analysis ### Subgroup analysis ### Subgroup analysis ### Subgroup analysis
### Subgroup analysis ### Subgroup analysis ### Subgroup analysis ### Subgroup analysis

names(conjoint_dataset)

# Political preferences

names(conjoint_dataset)

table(conjoint_dataset$pref_political_1)
table(conjoint_dataset$pref_party)

conjoint_dataset$pref_political_1 <- as.numeric(conjoint_dataset$pref_political_1)
conjoint_dataset$pref_party <- as.numeric(conjoint_dataset$pref_party)

conjoint_dataset_p1 <- conjoint_dataset %>% mutate(political_preferences=ifelse(pref_political_1>=0 & pref_political_1<4, "Left",
                                                                                ifelse(pref_political_1>=4 & pref_political_1<=6, "Center",
                                                                                       ifelse(pref_political_1>6 & pref_political_1<=10, "Right", NA))))


conjoint_dataset_p1$political_preferences <- factor(conjoint_dataset_p1$political_preferences,
                                                    levels = c("Left", "Center", "Right"))


table(conjoint_dataset_p1$political_preferences)

y <- cj(conjoint_dataset_p1, selected ~ 
          Reason_of_migration, 
        id = ~Response.ID, 
        estimate = "mm_diff", 
        by = ~political_preferences,
        weights = ~weight)

y$political_preferences <- y$BY

p1b <- plot(y, group = "political_preferences", vline = 0.0, feature_headers = FALSE) +
  scale_color_manual(values = c("black", "grey"), na.translate = F) +
  xlim(-0.2, 0.2) +
  theme_bw() +
  theme(legend.position = "bottom") +
  theme(legend.title = element_text(size = 0))

p1b

figure <- ggarrange(p1b, ncol = 1)  
figure

figure1p <- annotate_figure(figure, top = text_grob("A. Political preferences", 
                                                    color = "black", face = "bold", size = 14))


# Party preferences

# https://en.wikipedia.org/wiki/List_of_political_parties_in_the_Netherlands

names(conjoint_dataset)

table(conjoint_dataset$pref_party)


conjoint_dataset_p2 <- conjoint_dataset %>% mutate(party_preferences=ifelse(pref_party==1 | pref_party==2 | pref_party==3 | 
                                                                              pref_party==8 | pref_party==10 | pref_party==12 | 
                                                                              pref_party==13 | pref_party==16, "Right",
                                                                            ifelse(pref_party==4 | pref_party==5 | pref_party==6 | 
                                                                                     pref_party==7 | pref_party==9 | pref_party==14, "Left", NA)))




conjoint_dataset_p2$party_preferences <- factor(conjoint_dataset_p2$party_preferences,
                                                levels = c("Left", "Right"))


table(conjoint_dataset_p2$party_preferences)

y <- cj(conjoint_dataset_p2, selected ~ 
          Reason_of_migration, 
        id = ~Response.ID, 
        estimate = "mm_diff", 
        by = ~party_preferences,
        weights = ~weight)

y$party_preferences <- y$BY

p2b <- plot(y, group = "party_preferences", vline = 0.0, feature_headers = FALSE) +
  scale_color_manual(values = c("black", "grey"), na.translate = F) +
  xlim(-0.2, 0.2) +
  theme_bw() +
  theme(legend.position = "bottom") +
  theme(legend.title = element_text(size = 0))

p2b

figure <- ggarrange(p2b, ncol = 1)  
figure

figure2p <- annotate_figure(figure, top = text_grob("C. Party preferences: alternative coding", 
                                                    color = "black", face = "bold", size = 14))


# Party preferences: alternative coding left right, center

names(conjoint_dataset)

table(conjoint_dataset$pref_party)


conjoint_dataset_p3 <- conjoint_dataset %>% mutate(party_preferences_alternative=ifelse(pref_party==2 |
                                                                                          pref_party==8 |
                                                                                          pref_party==12 | 
                                                                                          pref_party==13, "Right",
                                                                                        ifelse(pref_party==5 | 
                                                                                                 pref_party==6 | 
                                                                                                 pref_party==9, "Left", 
                                                                                               ifelse(pref_party==1 | pref_party==3 |
                                                                                                        pref_party==4 | pref_party==7 |
                                                                                                        pref_party==10 | pref_party==11 |
                                                                                                        pref_party==14 | pref_party==15 |
                                                                                                        pref_party==16, "Center", NA))))




conjoint_dataset_p3$party_preferences_alternative <- factor(conjoint_dataset_p3$party_preferences_alternative,
                                                            levels = c("Left", "Center", "Right"))


table(conjoint_dataset_p3$party_preferences_alternative)

y <- cj(conjoint_dataset_p3, selected ~ 
          Reason_of_migration, 
        id = ~Response.ID, 
        estimate = "mm_diff", 
        by = ~party_preferences_alternative,
        weights = ~weight)

y$party_preferences_alternative <- y$BY

p3b <- plot(y, group = "party_preferences_alternative", vline = 0.0, feature_headers = FALSE) +
  scale_color_manual(values = c("black", "grey"), na.translate = F) +
  xlim(-0.2, 0.2) +
  theme_bw() +
  theme(legend.position = "bottom") +
  theme(legend.title = element_text(size = 0))

p3b

figure <- ggarrange(p3b, ncol = 1)  
figure

figure3p <- annotate_figure(figure, top = text_grob("B. Party preferences", 
                                                    color = "black", face = "bold", size = 14))


figure_joint <- ggarrange(figure1p, figure3p, figure2p, ncol = 1, common.legend = TRUE, legend = "bottom")  

figure_joint

ggsave(figure_joint, file = "subgroup_political.jpg", scale = 0.5, width = 12, height = 16)

#### Climate change preferences

names(conjoint_dataset)

# Climate change a serious problem

table(conjoint_dataset$pref_climate_1_1)

conjoint_dataset$pref_climate_1_1 <- as.numeric(conjoint_dataset$pref_climate_1_1)

table(conjoint_dataset$pref_climate_1_1)

hist(conjoint_dataset$pref_climate_1_1)

conjoint_dataset_c1 <- conjoint_dataset %>% mutate(climate_serious_problem=ifelse(pref_climate_1_1>=6 & pref_climate_1_1<=10, "Serious", 
                                                                                  ifelse(pref_climate_1_1>=0 & pref_climate_1_1<=5, "Not serious", NA)))


table(conjoint_dataset_c1$climate_serious_problem)

conjoint_dataset_c1$climate_serious_problem <- factor(conjoint_dataset_c1$climate_serious_problem,
                                                      levels = c("Serious", "Not serious"))

y <- cj(conjoint_dataset_c1, selected ~ 
          Reason_of_migration, 
        id = ~Response.ID, 
        estimate = "mm_diff", 
        by = ~climate_serious_problem,
        weights = ~weight)

y$climate_serious_problem <- y$BY

c1b <- plot(y, group = "climate_serious_problem", vline = 0.0, feature_headers = FALSE) +
  scale_color_manual(values = c("black"), na.translate = F) +
  xlim(-0.2, 0.2) +
  theme_bw() +
  theme(legend.position = "bottom") +
  theme(legend.title = element_text(size = 0))

figure <- ggarrange(c1b, ncol = 1)  
figure

figure1c <- annotate_figure(figure, top = text_grob("A. Climate change as a serious problem", 
                                                    color = "black", face = "bold", size = 12))

# Government to set ambitious climate targets

names(conjoint_dataset)

table(conjoint_dataset$pref_climate_2)

conjoint_dataset$pref_climate_2 <- as.numeric(conjoint_dataset$pref_climate_2)

table(conjoint_dataset$pref_climate_2)

hist(conjoint_dataset$pref_climate_2)

conjoint_dataset_c2 <- conjoint_dataset %>% mutate(ambitous_climate_targets=ifelse(pref_climate_2==1, "Very", 
                                                                                   ifelse(pref_climate_2==2, "Fairly", 
                                                                                          ifelse(pref_climate_2==3 | pref_climate_2==4, "Not", NA))))


table(conjoint_dataset_c2$ambitous_climate_targets)

conjoint_dataset_c2$ambitous_climate_targets <- factor(conjoint_dataset_c2$ambitous_climate_targets,
                                                       levels = c("Very", "Fairly", "Not"))


y <- cj(conjoint_dataset_c2, selected ~ 
          Reason_of_migration, 
        id = ~Response.ID, 
        estimate = "mm_diff", 
        by = ~ambitous_climate_targets,
        weights = ~weight)

y$ambitous_climate_targets <- y$BY

c2b <- plot(y, group = "ambitous_climate_targets", vline = 0.0, feature_headers = FALSE) +
  scale_color_manual(values = c("black", "grey"), na.translate = F) +
  xlim(-0.2, 0.2) +
  theme_bw() +
  theme(legend.position = "bottom") +
  theme(legend.title = element_text(size = 0))

figure <- ggarrange(c2b, ncol = 1)  
figure

figure2c <- annotate_figure(figure, top = text_grob("B. How important to set ambitious climate target", 
                                                    color = "black", face = "bold", size = 12))


# CLimate change man-made

names(conjoint_dataset)


conjoint_dataset$pref_climate_change_1 <- as.numeric(conjoint_dataset$pref_climate_change_1)
conjoint_dataset$pref_climate_change_2 <- as.numeric(conjoint_dataset$pref_climate_change_2)
conjoint_dataset$pref_climate_change_3 <- as.numeric(conjoint_dataset$pref_climate_change_3)

table(conjoint_dataset$pref_climate_change_1)
table(conjoint_dataset$pref_climate_change_2)
table(conjoint_dataset$pref_climate_change_3)

hist(conjoint_dataset$pref_climate_change_1)
hist(conjoint_dataset$pref_climate_change_2)
hist(conjoint_dataset$pref_climate_change_3)

conjoint_dataset_c3 <- conjoint_dataset %>% mutate(climate_change_human_made=ifelse(pref_climate_change_1>=6 & pref_climate_change_1<=10, "Yes", 
                                                                                    ifelse(pref_climate_change_1>=0 & pref_climate_change_1<=5, "No", NA)))


table(conjoint_dataset_c3$climate_change_human_made)

conjoint_dataset_c3$climate_change_human_made <- factor(conjoint_dataset_c3$climate_change_human_made,
                                                        levels = c("Yes", "No"))

y <- cj(conjoint_dataset_c3, selected ~ 
          Reason_of_migration, 
        id = ~Response.ID, 
        estimate = "mm_diff", 
        by = ~climate_change_human_made,
        weights = ~weight)

y$climate_change_human_made <- y$BY

c3b <- plot(y, group = "climate_change_human_made", vline = 0.0, feature_headers = FALSE) +
  scale_color_manual(values = c("black", "grey"), na.translate = F) +
  xlim(-0.2, 0.2) +
  theme_bw() +
  theme(legend.position = "bottom") +
  theme(legend.title = element_text(size = 0))

figure <- ggarrange(c3b, ncol = 1)  
figure

figure3c <- annotate_figure(figure, top = text_grob("C. Climate change as an anthropogenic phenonmenon", 
                                                    color = "black", face = "bold", size = 12))

# CLimate change has adverse effects

conjoint_dataset_c4 <- conjoint_dataset %>% mutate(climate_change_adverse_effects=ifelse(pref_climate_change_2>=6 & pref_climate_change_2<=10, "Yes", 
                                                                                         ifelse(pref_climate_change_2>=0 & pref_climate_change_2<=5, "No", NA)))


table(conjoint_dataset_c4$climate_change_adverse_effects)

conjoint_dataset_c4$climate_change_adverse_effects <- factor(conjoint_dataset_c4$climate_change_adverse_effects,
                                                             levels = c("Yes", "No"))

y <- cj(conjoint_dataset_c4, selected ~ 
          Reason_of_migration, 
        id = ~Response.ID, 
        estimate = "mm_diff", 
        by = ~climate_change_adverse_effects,
        weights = ~weight)

y$climate_change_adverse_effects <- y$BY

c4b <- plot(y, group = "climate_change_adverse_effects", vline = 0.0, feature_headers = FALSE) +
  scale_color_manual(values = c("black", "grey"), na.translate = F) +
  xlim(-0.2, 0.2) +
  theme_bw() +
  theme(legend.position = "bottom") +
  theme(legend.title = element_text(size = 0))

figure <- ggarrange(c4b, ncol = 1)  
figure

figure4c <- annotate_figure(figure, top = text_grob("D. Climate change has averse effects", 
                                                    color = "black", face = "bold", size = 12))

# CLimate mititation even if costly

conjoint_dataset_c5 <- conjoint_dataset %>% mutate(climate_change_mitiation_costly=ifelse(pref_climate_change_3>=6 & pref_climate_change_3<=10, "Yes", 
                                                                                          ifelse(pref_climate_change_3>=0 & pref_climate_change_3<=5, "No", NA)))


table(conjoint_dataset_c5$climate_change_mitiation_costly)

conjoint_dataset_c5$climate_change_mitiation_costly <- factor(conjoint_dataset_c5$climate_change_mitiation_costly,
                                                              levels = c("Yes", "No"))

y <- cj(conjoint_dataset_c5, selected ~ 
          Reason_of_migration, 
        id = ~Response.ID, 
        estimate = "mm_diff", 
        by = ~climate_change_mitiation_costly,
        weights = ~weight)

y$climate_change_mitiation_costly <- y$BY

c5b <- plot(y, group = "climate_change_mitiation_costly", vline = 0.0, feature_headers = FALSE) +
  scale_color_manual(values = c("black", "grey"), na.translate = F) +
  xlim(-0.2, 0.2) +
  theme_bw() +
  theme(legend.position = "bottom") +
  theme(legend.title = element_text(size = 0))

figure <- ggarrange(c5b, ncol = 1)  
figure

figure5c <- annotate_figure(figure, top = text_grob("E. Government should mitigate climate change even if costly", 
                                                    color = "black", face = "bold", size = 12))

# Adapt to climate change

names(conjoint_dataset)

conjoint_dataset$pref_climate_pol_1 <- as.numeric(conjoint_dataset$pref_climate_pol_1)
conjoint_dataset$pref_climate_pol_2 <- as.numeric(conjoint_dataset$pref_climate_pol_2)
conjoint_dataset$pref_climate_pol_3 <- as.numeric(conjoint_dataset$pref_climate_pol_3)

table(conjoint_dataset$pref_climate_pol_1)
table(conjoint_dataset$pref_climate_pol_2)
table(conjoint_dataset$pref_climate_pol_3)

hist(conjoint_dataset$pref_climate_pol_1)
hist(conjoint_dataset$pref_climate_pol_2)
hist(conjoint_dataset$pref_climate_pol_3)

conjoint_dataset_c5 <- conjoint_dataset %>% mutate(climate_adaptation=ifelse(pref_climate_pol_1==1, "Yes", 
                                                                             ifelse(pref_climate_pol_1==2, "No", NA)))


table(conjoint_dataset_c5$climate_adaptation)

conjoint_dataset_c5$climate_adaptation <- factor(conjoint_dataset_c5$climate_adaptation,
                                                 levels = c("Yes", "No"))

y <- cj(conjoint_dataset_c5, selected ~ 
          Reason_of_migration, 
        id = ~Response.ID, 
        estimate = "mm_diff", 
        by = ~climate_adaptation,
        weights = ~weight)

y$climate_adaptation <- y$BY

c6b <- plot(y, group = "climate_adaptation", vline = 0.0, feature_headers = FALSE) +
  scale_color_manual(values = c("black", "grey"), na.translate = F) +
  xlim(-0.2, 0.2) +
  theme_bw() +
  theme(legend.position = "bottom") +
  theme(legend.title = element_text(size = 0))

figure <- ggarrange(c6b, ncol = 1)  
figure

figure6c <- annotate_figure(figure, top = text_grob("F. Adapt your own behavior to reduce emissions", 
                                                    color = "black", face = "bold", size = 12))

# Adapt to climate change

names(conjoint_dataset)

table(conjoint_dataset$pref_climate_pol_1)
table(conjoint_dataset$pref_climate_pol_2)
table(conjoint_dataset$pref_climate_pol_3)

hist(conjoint_dataset$pref_climate_pol_1)
hist(conjoint_dataset$pref_climate_pol_2)
hist(conjoint_dataset$pref_climate_pol_3)

conjoint_dataset_c6 <- conjoint_dataset %>% mutate(climate_invest_in_firms=ifelse(pref_climate_pol_2==1 | pref_climate_pol_3==1, "Yes", 
                                                                                  ifelse(pref_climate_pol_2==2 & pref_climate_pol_3==2, "No", NA)))


table(conjoint_dataset_c6$climate_invest_in_firms)

conjoint_dataset_c6$climate_invest_in_firms <- factor(conjoint_dataset_c6$climate_invest_in_firms,
                                                      levels = c("Yes", "No"))


y <- cj(conjoint_dataset_c6, selected ~ 
          Reason_of_migration, 
        id = ~Response.ID, 
        estimate = "mm_diff", 
        by = ~climate_invest_in_firms,
        weights = ~weight)

y$climate_invest_in_firms <- y$BY

c7b <- plot(y, group = "climate_invest_in_firms", vline = 0.0, feature_headers = FALSE) +
  scale_color_manual(values = c("black", "grey"), na.translate = F) +
  xlim(-0.2, 0.2) +
  theme_bw() +
  theme(legend.position = "bottom") +
  theme(legend.title = element_text(size = 0))

figure <- ggarrange(c7b, ncol = 1)  
figure

figure7c <- annotate_figure(figure, top = text_grob("G. Willingness to invest in high-carbon footprint companies", 
                                                    color = "black", face = "bold", size = 12))

figure_joint <- ggarrange(figure1c, figure2c, figure3c,
                         figure4c, figure5c, figure6c, figure7c,
                         ncol = 2, nrow = 4, common.legend = TRUE, legend = "bottom")  

figure_joint

ggsave(figure_joint, file = "subgroup_climate.jpg", scale = 0.35, width = 28, height = 30)


#### ALTERNATIVE: Climate change preferences

names(conjoint_dataset)

# Climate change a serious problem

table(conjoint_dataset$pref_climate_1_1)

conjoint_dataset$pref_climate_1_1 <- as.numeric(conjoint_dataset$pref_climate_1_1)

table(conjoint_dataset$pref_climate_1_1)

hist(conjoint_dataset$pref_climate_1_1)

conjoint_dataset_c1 <- conjoint_dataset %>% mutate(climate_serious_problem=ifelse(pref_climate_1_1>=7 & pref_climate_1_1<=10, "Serious", 
                                                                                  ifelse(pref_climate_1_1>=0 & pref_climate_1_1<=3, "Not serious", NA)))


table(conjoint_dataset_c1$climate_serious_problem)

conjoint_dataset_c1$climate_serious_problem <- factor(conjoint_dataset_c1$climate_serious_problem,
                                                      levels = c("Serious", "Not serious"))

y <- cj(conjoint_dataset_c1, selected ~ 
          Reason_of_migration, 
        id = ~Response.ID, 
        estimate = "mm_diff", 
        by = ~climate_serious_problem,
        weights = ~weight)

y$climate_serious_problem <- y$BY

c1b <- plot(y, group = "climate_serious_problem", vline = 0.0, feature_headers = FALSE) +
  scale_color_manual(values = c("black"), na.translate = F) +
  xlim(-0.2, 0.2) +
  theme_bw() +
  theme(legend.position = "bottom") +
  theme(legend.title = element_text(size = 0))

figure <- ggarrange(c1b, ncol = 1)  
figure

figure1c <- annotate_figure(figure, top = text_grob("A. Climate change as a serious problem", 
                                                    color = "black", face = "bold", size = 12))

# CLimate change man-made
names(conjoint_dataset)


conjoint_dataset$pref_climate_change_1 <- as.numeric(conjoint_dataset$pref_climate_change_1)
conjoint_dataset$pref_climate_change_2 <- as.numeric(conjoint_dataset$pref_climate_change_2)
conjoint_dataset$pref_climate_change_3 <- as.numeric(conjoint_dataset$pref_climate_change_3)

table(conjoint_dataset$pref_climate_change_1)
table(conjoint_dataset$pref_climate_change_2)
table(conjoint_dataset$pref_climate_change_3)

hist(conjoint_dataset$pref_climate_change_1)
hist(conjoint_dataset$pref_climate_change_2)
hist(conjoint_dataset$pref_climate_change_3)

conjoint_dataset_c3 <- conjoint_dataset %>% mutate(climate_change_human_made=ifelse(pref_climate_change_1>=7 & pref_climate_change_1<=10, "Yes", 
                                                                                    ifelse(pref_climate_change_1>=0 & pref_climate_change_1<=3, "No", NA)))


table(conjoint_dataset_c3$climate_change_human_made)

conjoint_dataset_c3$climate_change_human_made <- factor(conjoint_dataset_c3$climate_change_human_made,
                                                        levels = c("Yes", "No"))

y <- cj(conjoint_dataset_c3, selected ~ 
          Reason_of_migration, 
        id = ~Response.ID, 
        estimate = "mm_diff", 
        by = ~climate_change_human_made,
        weights = ~weight)

y$climate_change_human_made <- y$BY

c3b <- plot(y, group = "climate_change_human_made", vline = 0.0, feature_headers = FALSE) +
  scale_color_manual(values = c("black", "grey"), na.translate = F) +
  xlim(-0.2, 0.2) +
  theme_bw() +
  theme(legend.position = "bottom") +
  theme(legend.title = element_text(size = 0))

figure <- ggarrange(c3b, ncol = 1)  
figure

figure3c <- annotate_figure(figure, top = text_grob("B. Climate change as an anthropogenic phenonmenon", 
                                                    color = "black", face = "bold", size = 12))

# CLimate change has adverse effects

conjoint_dataset_c4 <- conjoint_dataset %>% mutate(climate_change_adverse_effects=ifelse(pref_climate_change_2>=7 & pref_climate_change_2<=10, "Yes", 
                                                                                         ifelse(pref_climate_change_2>=0 & pref_climate_change_2<=3, "No", NA)))


table(conjoint_dataset_c4$climate_change_adverse_effects)

conjoint_dataset_c4$climate_change_adverse_effects <- factor(conjoint_dataset_c4$climate_change_adverse_effects,
                                                             levels = c("Yes", "No"))

y <- cj(conjoint_dataset_c4, selected ~ 
          Reason_of_migration, 
        id = ~Response.ID, 
        estimate = "mm_diff", 
        by = ~climate_change_adverse_effects,
        weights = ~weight)

y$climate_change_adverse_effects <- y$BY

c4b <- plot(y, group = "climate_change_adverse_effects", vline = 0.0, feature_headers = FALSE) +
  scale_color_manual(values = c("black", "grey"), na.translate = F) +
  xlim(-0.2, 0.2) +
  theme_bw() +
  theme(legend.position = "bottom") +
  theme(legend.title = element_text(size = 0))

figure <- ggarrange(c4b, ncol = 1)  
figure

figure4c <- annotate_figure(figure, top = text_grob("C. Climate change has averse effects", 
                                                    color = "black", face = "bold", size = 12))


# CLimate mititation even if costly

conjoint_dataset_c5 <- conjoint_dataset %>% mutate(climate_change_mitiation_costly=ifelse(pref_climate_change_3>=7 & pref_climate_change_3<=10, "Yes", 
                                                                                          ifelse(pref_climate_change_3>=0 & pref_climate_change_3<=3, "No", NA)))


table(conjoint_dataset_c5$climate_change_mitiation_costly)

conjoint_dataset_c5$climate_change_mitiation_costly <- factor(conjoint_dataset_c5$climate_change_mitiation_costly,
                                                              levels = c("Yes", "No"))

y <- cj(conjoint_dataset_c5, selected ~ 
          Reason_of_migration, 
        id = ~Response.ID, 
        estimate = "mm_diff", 
        by = ~climate_change_mitiation_costly,
        weights = ~weight)

y$climate_change_mitiation_costly <- y$BY

c5b <- plot(y, group = "climate_change_mitiation_costly", vline = 0.0, feature_headers = FALSE) +
  scale_color_manual(values = c("black", "grey"), na.translate = F) +
  xlim(-0.2, 0.2) +
  theme_bw() +
  theme(legend.position = "bottom") +
  theme(legend.title = element_text(size = 0))

figure <- ggarrange(c5b, ncol = 1)  
figure

figure5c <- annotate_figure(figure, top = text_grob("D. Government should mitigate climate change even if costly", 
                                                    color = "black", face = "bold", size = 12))

figurecombo <- ggarrange(figure1c, figure3c,
                         figure4c, figure5c, 
                         ncol = 2, nrow = 2, common.legend = TRUE, legend = "bottom")  

figurecombo

ggsave(figurecombo, file = "subgroup_climate_altrnative.jpg", scale = 0.35, width = 28, height = 20)


##### Demographics for the table in the supplementary material

sumtable(conjoint_dataset, 
         vars = c("gender_cat", 
                  "age_cat", 
                  "place_cat"),
         digits = 4)

##### END ##### END ##### END ##### END ##### END ##### END ##### END  ##### 
##### END ##### END ##### END ##### END ##### END ##### END ##### END  ##### 
##### END ##### END ##### END ##### END ##### END ##### END ##### END  ##### 

